package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resources;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.adm.model.service.ModuleDAO;
import com.erpanderp.wam.adm.model.service.ResourceDAO;
import com.erpanderp.wam.usm.model.entity.projectandassignment.Project;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@SuppressWarnings("unused")
@ManagedBean
@ViewScoped
public class CreateResourceBean implements Serializable {
	private static final long serialVersionUID = 1L;
	private Map<String, Long> modulesMap = new HashMap<String, Long>();
	ModuleDAO moduleDAO = new ModuleDAO();
	boolean FORM_VALID;

	private List<Resource> resources = new ArrayList<Resource>();
	private Resource resource = new Resource();
	ResourceDAO resourceDAO = new ResourceDAO();
	private Long selectedModuleId;
	private boolean renderResourceTable;

	public CreateResourceBean() {
		List<Module> modules = moduleDAO.getAllModules();
		for (Module module : modules) {
			modulesMap.put(module.getModuleName(), module.getModuleId());
		}
	}

	// ACTIONS FOR RESOURCE
	public String createAnotherResourceAction() {
		System.out.println("createAnotherResourceAction...............");
		validateForm();

		if (FORM_VALID) {
			setRenderResourceTable(true);
			resource.setCreateDate(new Date());
			resource.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
			resource.setStatus(CommonLookup.STATUS_ACTIVE);
			resource.setModule(moduleDAO.getModuleById(selectedModuleId));
			resources.add(resource);
			resource = new Resource();
		}

		return null;
	}

	public String saveResourceAction() {
		Date now = new Date();
		try{
			if (resources.size() > 0 && (resource.getResourceName() == null || resource.getResourceName().equals(""))) {
				resourceDAO.saveResources(resources);
			}

			if (resources.size() == 0) {
				validateForm();
				if (FORM_VALID) {
					resource.setCreateDate(now);
					resource.setCreatedBy("DEVELOPER");
					resource.setStatus("ACTIVE");
					resource.setModule(moduleDAO.getModuleById(selectedModuleId));
					resourceDAO.createResource(resource);
					resource = new Resource();
				}
			}
			resources.clear();
			FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "Resource(s) created successfully.", ""));
		}catch (Exception e) {
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed to create resource.", e.getMessage()));
		}
	
		return null;
	}

	private void validateForm() {
		FORM_VALID = true;

		if (ValidationUtil.isEmpty(resource.getResourceName(), "Resource Name",
				null, FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}
		if (ValidationUtil.isEmpty(resource.getFileName(), " FileName", null,
				FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}
		if (ValidationUtil.isEmpty(resource.getDisplayName(), "DisplayName",
				null, FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}

	}

	// GETTERS AND SETTERS OF INPUT FIELDS

	public List<Resource> getResources() {
		return resources;
	}

	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}

	public Map<String, Long> getModulesMap() {
		return modulesMap;
	}

	public void setModulesMap(Map<String, Long> modulesMap) {
		this.modulesMap = modulesMap;

	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public boolean isRenderResourceTable() {
		return renderResourceTable;
	}

	public void setRenderResourceTable(boolean renderResourceTable) {
		this.renderResourceTable = renderResourceTable;
	}
	public Long getSelectedModuleId() {
		return selectedModuleId;
	}

	public void setSelectedModuleId(Long selectedModuleId) {
		this.selectedModuleId = selectedModuleId;
	}

}
